<template>
  <div class="orderItemMenuWrapper">
    <slot/>
    <el-dropdown-item v-if="isCancelFlag">
      <el-button type="text" @click="fnCancel(row)">取消订单</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isToPayFlag">
      <el-button type="text" @click="fnToPay(row)">前往付款</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isRefundFlag">
      <el-button type="text" @click="fnRefund(row)">申请退款</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isCancelRefundFlag">
      <el-button type="text" @click="fnCancelRefund(row)">取消退款</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isTranceFlag">
      <el-button type="text" @click="fnTrance(row)">查看物流</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isTakeDeliveryFlag">
      <el-button type="text" @click="fnTakeDelivery(row)">确定收货</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isReviewFlag">
      <el-button type="text" @click="fnReview(row)">订单评论</el-button>
    </el-dropdown-item>
    <el-dropdown-item v-if="isRemoveFlag">
      <el-button type="text" style="color: red;" @click="fnRemove(row)">删除订单</el-button>
    </el-dropdown-item>
  </div>
</template>

<script>
import {
  applyRefundByOrderId,
  applyRefundCancelByOrderId,
  cancelOrderByIds,
  payOrderById, removeOrderUserById
} from '@/api/ApiOrderDetail'
import { ResultCode } from '@/constants/constants'
import { takeDeliveryByOrderId } from '@/api/ApiGoodDeliver'
import OrderStateCode from '@/constants/OrderStateCode'

export default {
  name: 'OrderItemMenu',
  props: {
    row: {
      type: Object,
      required: true
    }
  },
  computed: {
    orderState () {
      return this.row && this.row.id && this.row.state ? this.row.state : null
    },
    /**
     * 取消订单，支付前可以调用
     */
    isCancelFlag () {
      return this.orderState === OrderStateCode.STATE_TO_PAY
    },
    /**
     * 订单支付，未支付可以调用
     */
    isToPayFlag () {
      return this.orderState === OrderStateCode.STATE_TO_PAY
    },
    /**
     * 申请退款，支付后未发货可以调用
     */
    isRefundFlag () {
      return this.orderState === OrderStateCode.STATE_TO_TRANS
    },
    /**
     * 取消退款申请，退款中可以调用
     */
    isCancelRefundFlag () {
      return this.orderState === OrderStateCode.STATE_TO_REFUND
    },
    /**
     * 查看物流，不是待支付和代发货，存在物流号
     */
    isTranceFlag () {
      return this.orderState !== OrderStateCode.STATE_TO_PAY &&
        this.orderState !== OrderStateCode.STATE_TO_TRANS && this.row['logisticsNumber']
    },
    /**
     * 收货，运输中 待签收
     */
    isTakeDeliveryFlag () {
      return this.orderState === OrderStateCode.STATE_TO_RECEIVE ||
        this.orderState === OrderStateCode.STATE_TO_TRANSPORTING
    },
    /**
     * 订单评论，收货后
     */
    isReviewFlag () {
      return this.orderState === OrderStateCode.STATE_RECEIVED ||
        this.orderState === OrderStateCode.STATE_END
    },
    /**
     * 移除订单，完结订单
     */
    isRemoveFlag () {
      return this.orderState === OrderStateCode.STATE_RECEIVED ||
        this.orderState === OrderStateCode.STATE_REFUNDED ||
        this.orderState === OrderStateCode.STATE_CANCEL ||
        this.orderState === OrderStateCode.STATE_END
    }
  },
  methods: {
    /**
     * 发送change事件，接收后刷新列表
     */
    sendChangeMessage () {
      this.$emit('change', this.row)
    },
    /**
     * 取消订单，未支付订单可以调用
     * @param row 订单详细数据
     */
    fnCancel (row) {
      if (row && row.id) {
        this.$confirm('是否确定取消订单？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          cancelOrderByIds([row.id], data => {
            if (data && data.code === ResultCode.OK) {
              this.$message.success('取消订单成功')
              this.sendChangeMessage()
            }
          })
        }).catch(() => {
        })
      } else {
        this.$message.error('数据错误')
      }
    },
    /**
     * 前往付款，未支付订单可以调用
     * @param row 订单详细数据
     */
    fnToPay (row) {
      if (row && row.id) {
        payOrderById(row.id)
      } else {
        this.$message.error('数据错误')
      }
    },
    /**
     * 申请退款，已付款未发货可以调用，调用后订单变为退款中
     * @param row 订单详细数据
     */
    fnRefund (row) {
      if (row && row.id) {
        this.$prompt('请输入申请退款理由', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^.+$/,
          inputErrorMessage: '理由不能为空'
        }).then(({ value }) => {
          applyRefundByOrderId(row.id, value, data => {
            if (data && data.code === ResultCode.OK) {
              this.$message.success('申请退款成功，等待商家退款')
              this.sendChangeMessage()
            }
          })
        }).catch(() => {
        })
      } else {
        this.$message.error('数据错误')
      }
    },
    /**
     * 取消申请退款，退款中的订单可以取消申请退款，取消后回到代发货状态
     * @param row 订单详细数据
     */
    fnCancelRefund (row) {
      if (row && row.id) {
        this.$confirm('是否确定取消退款申请？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          applyRefundCancelByOrderId(row.id, data => {
            if (data && data.code === ResultCode.OK) {
              this.$message.success('取消退款申请成功')
              this.sendChangeMessage()
            }
          })
        }).catch(() => {
        })
      } else {
        this.$message.error('数据错误')
      }
    },
    /**
     * 查看物流，发货后调用，即状态为：运输中 待签收 已签收 已完结
     * @param row 订单详细数据
     */
    fnTrance (row) {
      this.$emit('click', 'trance', row)
    },
    /**
     * 订单评论，已签收 已完结调用
     * @param row 订单详细数据
     */
    fnReview (row) {
      this.$emit('click', 'review', row)
    },
    /**
     * 确定收货，运输中 代签收的订单可以收货
     * @param row 订单详细数据
     */
    fnTakeDelivery (row) {
      if (row && row.id) {
        this.$confirm('是否确定收货？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          takeDeliveryByOrderId(row.id, data => {
            if (data && data.code === ResultCode.OK) {
              this.$message.success('收货成功')
              this.sendChangeMessage()
            }
          })
        }).catch(() => {
        })
      } else {
        this.$message.error('数据错误')
      }
    },
    /**
     * 删除订单，已签收 已退款 已取消 已完结 可以调用
     * @param row 订单详细数据
     */
    fnRemove (row) {
      if (row && row.id) {
        this.$confirm('是否确定删除该订单？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          removeOrderUserById(row.id, data => {
            if (data && data.code === ResultCode.OK) {
              this.$message.success('移除订单成功')
              this.sendChangeMessage()
            }
          })
        }).catch(() => {
        })
      } else {
        this.$message.error('数据错误')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.orderItemMenuWrapper{

}
</style>
